/*!
 * \file t__split_eq_string.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


typedef struct st_test {
    char *p_data;
    char *p_value;
} test_st;

static void __my_list_node_free(Frlist_node *node)
{
    frfree(((test_st *)node->data)->p_data);
    frfree(((test_st *)node->data)->p_value);
    frfree((test_st *)node->data);
    frfree(node->p_key);
}


static void __case1(void)
{
    Frhash_header *p_hash_hdr = NULL;
    int i_ret = 0;
    Frlist_node *p_st_node_add = NULL;
    unsigned long ul_hash_key = 0;
    test_st *p_node = NULL;

    p_node = (test_st *)frmalloc(sizeof(test_st));

    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    p_st_node_add = frlist_node_create(p_node);
    i_ret = frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node_add);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    __my_list_node_free(p_st_node_add);
    frfree(p_st_node_add);

}

static void __case2(void)
{
    Frhash_header *p_hash_hdr = NULL;
    int i_ret = 0;
    Frlist_node *p_st_node_add = NULL;
    unsigned long ul_hash_key = 0;
    test_st *p_node = NULL;

    p_node = (test_st *)frmalloc(sizeof(test_st));

    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    p_st_node_add = frlist_node_create(p_node);
    i_ret = frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node_add);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    __my_list_node_free(p_st_node_add);
    frfree(p_st_node_add);
    frfree(p_hash_hdr);

}

static void __case3(void)
{
    Frhash_header *p_hash_hdr = NULL;
    unsigned long ul_hash_size = 5;
    int i_ret = 0;
    Frlist_node *p_st_node_add = NULL;
    unsigned long ul_hash_key = 0;
    test_st *p_node = NULL;
    char *p_data_cmp =  NULL;

    p_node = (test_st *)frmalloc(sizeof(test_st));

    p_node->p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_node->p_data,strlen("test") + 1,"%s","test");
    p_node->p_value = (char *)frmalloc(strlen("test1") + 1);
    snprintf(p_node->p_value,strlen("test1") + 1,"%s","test1");

    p_st_node_add = frlist_node_create((test_st *)p_node);

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);
    i_ret = frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node_add);

    p_data_cmp = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data_cmp,strlen("test") + 1,"%s","test");

    p_st_node_add = frhash2_getLong(p_hash_hdr,ul_hash_key);

    CU_ASSERT_STRING_EQUAL(((test_st *)p_st_node_add->data)->p_value,"test1");
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frhash2_free(p_hash_hdr, __my_list_node_free);
    frfree(p_data_cmp);
    frfree(p_hash_hdr);

}

static void __case4(void)
{
    Frhash_header *p_hash_hdr = NULL;
    unsigned long ul_hash_size = 5;
    int i_ret = 0;
    Frlist_node *p_st_node_add = NULL;
    unsigned long ul_hash_key = 0;

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);

    i_ret = frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node_add);

    CU_ASSERT_EQUAL(i_ret,FR_FAIL);

    frhash2_free(p_hash_hdr, __my_list_node_free);
    frfree(p_hash_hdr);

}

static void __case5(void)
{
    Frhash_header *p_hash_hdr = NULL;
    unsigned long ul_hash_size = 5;
    int i_ret = 0;
    Frlist_node *p_st_node_add = NULL;
    unsigned long ul_hash_key = 0;
    test_st *p_node = NULL;
    char *p_data_cmp =  NULL;

    p_node = (test_st *)frmalloc(sizeof(test_st));

    p_node->p_data = (char *)frmalloc( 1);
    snprintf(p_node->p_data,1,"%s","\0");
    p_node->p_value = (char *)frmalloc(strlen("test2") + 1);
    snprintf(p_node->p_value,strlen("test2") + 1,"%s","test2");

    p_st_node_add = frlist_node_create(p_node);

    p_hash_hdr = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    frhash2_create(p_hash_hdr,ul_hash_size);
    i_ret = frhash2_addLong(p_hash_hdr,ul_hash_key,p_st_node_add);

    p_data_cmp = (char *)frmalloc( 1 );
    snprintf(p_data_cmp, 1,"%s","\0");

    p_st_node_add = frhash2_getLong(p_hash_hdr,ul_hash_key);

    CU_ASSERT_STRING_EQUAL(((test_st *)p_st_node_add->data)->p_value, "test2");
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frhash2_free(p_hash_hdr, __my_list_node_free);
    frfree(p_hash_hdr);
    frfree(p_data_cmp);

}

int add_suite_frhash2_addLong(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frhash2 frhash2_addLong()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);

    return FR_SUCC;
}
